package com.cs171.market.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cs171.market.Repository.ExpressRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author yangyifei
 * @version 1.0
 * @date 2020/6/20 23:48
 */
@CrossOrigin
@RestController
public class ExpressController {
    @Autowired
    private ExpressRepository expressRepository;

    @GetMapping("kuaidi/1106975712662")
    public Object getOrders(){
        JSONObject result = jsonhead();
        JSONArray oAll = (JSONArray) JSON.toJSON(expressRepository.findAll());
        result.put("data",oAll);
        return result;
    }

    /**
     * 重构代码，相似的Json Head
     * @return JsonObject
     */
    private JSONObject jsonhead(){
        JSONObject pAll = new JSONObject();
        JSONObject pSuccess = new JSONObject();
        pSuccess.put("msg", "获取物流信息成功");
        pSuccess.put("status", "200");
        pAll.put("meta", pSuccess);
        return pAll;
    }
}
